import heapq
from collections import defaultdict

n, m = map(int, input().split())
graph = defaultdict(list)
for _ in range(m):
    u, v, w = map(int, input().split())
    graph[u].append((v, w))
distance = [float('inf')] * (n + 1)
distance[1] = 0
heap = [(0, 1)]  # 距离，节点编号

while heap:
    dis, curr_index = heapq.heappop(heap)
    for next_e, weight in graph[curr_index]:
        distance[next_e] = min(distance[next_e], dis + weight)
        heapq.heappush(heap, (distance[next_e], next_e))

for i in range(1, n + 1):
    if distance[i] != float('inf'):
        print(distance[i], end=' ')
    else:
        print(-1, end=' ')
